/*******************************************************************************
 * Copyright (c) 2012 AwOiSoAk.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     $AwOiSoAk - initial API and implementation
 ******************************************************************************/
package com.awoisoak.dundeeguide;

import java.util.List;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class Map extends MapActivity {
	
	//Ids for option menu
	static final private int CHANGE_MAP= Menu.FIRST;
	static final private int MAIN_MENU= Menu.FIRST+1;
	boolean satellite;// To manage the kind of map
	MapView mapView;
	
	//Variables for the overlay (different ItemizedOverlays and Drawables for each kind of "point of interest" )
	List<Overlay> mapOverlays;
	Drawable drawable;
	Drawable drawableAttractions;
	Drawable drawableRestaurants;
	Drawable drawableNightlifes;
	MyItemizedOverlay itemizedOverlay;
	MyItemizedOverlay itemizedOverlayAttractions;
	MyItemizedOverlay itemizedOverlayRestaurants;
	MyItemizedOverlay itemizedOverlayNightlifes;

	

	String coord = null;
	String place = null;
	int explanation = 0;
	MapController mapController;
	GeoPoint geop;

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map);

		Bundle extras = getIntent().getExtras();
		
		if (extras!=null)
			//We have to capture these possible exception because some variables could not exist 
			//It will depend on which Activity the user was coming from
			
			try{
				coord = extras.getString("tag_coordinate");		
				place = extras.getString("tag_place");

			}catch (NullPointerException e){e.printStackTrace();}
		
		
		//Map
		mapView  = (MapView)findViewById(R.id.map);
		mapView.setBuiltInZoomControls(true);
		mapController = mapView.getController();
		mapView.setSatellite(true);
		satellite= true;
		

		//Map Overlay
		mapOverlays = mapView.getOverlays();
		
		


		
		
		try{
			//We have pressed on Map at the Main menu ("Whole map")
			if (coord.equals("0,0")) {
				
				//We got an icon for each specific layer (itemizedOverlay)
				//We pass "this" when we create the itemizedOverlays in order to be able to use dialogs in MyItemizedOverlay.onTap
				drawableAttractions = this.getResources().getDrawable(R.drawable.map_attraction);
				itemizedOverlayAttractions = new MyItemizedOverlay(drawableAttractions,this); 
				
				drawableRestaurants = this.getResources().getDrawable(R.drawable.map_restaurant);
				itemizedOverlayRestaurants = new MyItemizedOverlay(drawableRestaurants,this);

				drawableNightlifes = this.getResources().getDrawable(R.drawable.map_nightlife);
				itemizedOverlayNightlifes = new MyItemizedOverlay(drawableNightlifes,this);
				
				//We add all the items to our layers()
				wholeMap();
				
				//We add the layers to our map
				mapOverlays.add(itemizedOverlayAttractions);
				mapOverlays.add(itemizedOverlayRestaurants);
				mapOverlays.add(itemizedOverlayNightlifes);
	
			}
			//We have pressed on Map for an specific point of interest ("Partial map")
			else{
				
				//We got an icon for our new layer 
				drawable = this.getResources().getDrawable(R.drawable.ic_arrow);
				itemizedOverlay = new MyItemizedOverlay(drawable,this);
				
				//We add the specific item to our layer
				partialMap();
				
				//We add the layer to our map
				mapOverlays.add(itemizedOverlay);
			}


		}catch (NullPointerException e){e.printStackTrace();} 
		Toast.makeText(Map.this, "Press the Menu button to change the map style", Toast.LENGTH_LONG).show();

}
	
	
	
	
	public void wholeMap()
	{	
		//ATTRACTIONS
		GeoPoint point2 = new GeoPoint(56469748,-2989334);
		OverlayItem overlayitem2 = new OverlayItem(point2, "Dundee Law", "56.469748,-2.989334");
		itemizedOverlayAttractions.addOverlay(overlayitem2);
		
		
		GeoPoint point3 = new GeoPoint(56462608,-2971165);
		OverlayItem overlayitem3 = new OverlayItem(point3, "Mc Manus Galleries", "56.462608,-2.971165");
		itemizedOverlayAttractions.addOverlay(overlayitem3);
		
		GeoPoint point4 = new GeoPoint(56456848,-2968358);
		OverlayItem overlayitem4 = new OverlayItem(point4, "Discovery Point", "56.456848,-2.968358");
		itemizedOverlayAttractions.addOverlay(overlayitem4);
		
		
		GeoPoint point5 = new GeoPoint(56460628,-2970041);
		OverlayItem overlayitem5 = new OverlayItem(point5, "High Street", "56.460628,-2.970041");
		itemizedOverlayAttractions.addOverlay(overlayitem5);
		
		GeoPoint point6 = new GeoPoint(56462976,-2870085);
		OverlayItem overlayitem6 = new OverlayItem(point6, "Broughty Ferry Castle", "56.462976,-2.870085");
		itemizedOverlayAttractions.addOverlay(overlayitem6);
		
		
		
		
		//RESTAURANTS		
		GeoPoint point7 = new GeoPoint(56459441,-2980142);
		OverlayItem overlayitem7 = new OverlayItem(point7, "The Byzantium", "56.459441,-2.980142");
		itemizedOverlayRestaurants.addOverlay(overlayitem7);
		
		GeoPoint point8 = new GeoPoint(5646093,-29657);
		OverlayItem overlayitem8 = new OverlayItem(point8, "Marco Polo", "56.46093,-2.9657");
		itemizedOverlayRestaurants.addOverlay(overlayitem8);
		
		GeoPoint point9 = new GeoPoint(56461082,-2986007);
		OverlayItem overlayitem9 = new OverlayItem(point9, "Beiderbeckes", "56.461082,-2.986007");
		itemizedOverlayRestaurants.addOverlay(overlayitem9);
		
		GeoPoint point10 = new GeoPoint(56461162,-2970873);
		OverlayItem overlayitem10 = new OverlayItem(point10, "Pancake Place", "56.461162,-2.970873");
		itemizedOverlayRestaurants.addOverlay(overlayitem10);
		
		GeoPoint point11 = new GeoPoint(56481442,-3021983);
		OverlayItem overlayitem11 = new OverlayItem(point11, "Shehzad Balti Tandoori", "56.481442,-3.021983");
		itemizedOverlayRestaurants.addOverlay(overlayitem11);
		
		GeoPoint point12 = new GeoPoint(56460992,-2966922);
		OverlayItem overlayitem12 = new OverlayItem(point12, "RanchoPancho", "56.460992,-2.966922");
		itemizedOverlayRestaurants.addOverlay(overlayitem12);
		
		//NIGHTLIFE
		GeoPoint point13 = new GeoPoint(56460588,-2977028);
		OverlayItem overlayitem13 = new OverlayItem(point13, "Fat Sam's", "56.460588,-2.977028");
		itemizedOverlayNightlifes.addOverlay(overlayitem13);
		//Set Zoom and centre the map
		mapController.animateTo(point13);
		mapController.setZoom(15);

		GeoPoint point14 = new GeoPoint(56457879,-2982356);
		OverlayItem overlayitem14 = new OverlayItem(point14, "DUSA The Union", "56.457879,-2.982356");
		itemizedOverlayNightlifes.addOverlay(overlayitem14);

		GeoPoint point15 = new GeoPoint(56464969,-2961711);
		OverlayItem overlayitem15 = new OverlayItem(point15, "Reading Rooms", "56.464969,-2.961711");
		itemizedOverlayNightlifes.addOverlay(overlayitem15);
		
		GeoPoint point16 = new GeoPoint(56460514,-2976719);
		OverlayItem overlayitem16 = new OverlayItem(point16, "Liquid and Envy", "56.460514,-2.976719");
		itemizedOverlayNightlifes.addOverlay(overlayitem16);
		
		GeoPoint point17 = new GeoPoint(56460887,-2976112);
		OverlayItem overlayitem17 = new OverlayItem(point17, "Beat Generator", "56.460887,-2.976112");
		itemizedOverlayNightlifes.addOverlay(overlayitem17);
		
		GeoPoint point18 = new GeoPoint(56460992,-2966922);
		OverlayItem overlayitem18 = new OverlayItem(point18, "The Dog House", "56.460992,-2.966922");
		itemizedOverlayNightlifes.addOverlay(overlayitem18);
		
		


	}

	public void partialMap()
	{
		Double latitude = Double.parseDouble(coord.split(",")[0].toString());
		Double longitude = Double.parseDouble(coord.split(",")[1].toString());;
		
		geop = new GeoPoint	((int)(latitude*1E6),(int)(longitude*1E6));
		OverlayItem overlayitem = new OverlayItem(geop, place, coord);
		itemizedOverlay.addOverlay(overlayitem);
		mapController.animateTo(geop);
		mapController.setZoom(19);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		//Create and add new menu items
		MenuItem itemMap = menu.add (0, CHANGE_MAP, Menu.NONE, R.string.change_map );
		MenuItem itemMainMenu = menu.add (0, MAIN_MENU, Menu.NONE, R.string.main_menu);	
		
		//Assign icons
		itemMap.setIcon(android.R.drawable.ic_dialog_map);
		itemMainMenu.setIcon(android.R.drawable.ic_media_previous);
		
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		
		switch (item.getItemId()){
		
		case (CHANGE_MAP):{
			if (satellite==true){
				mapView.setSatellite(false);
				satellite=false;
				}
			else {
				mapView.setSatellite(true);
				satellite=true;
				}
			
			return true;
			}
		
		
		case (MAIN_MENU):{
			Intent i = new Intent (Map.this,Main.class);
			startActivity(i);
			return true;
			}
		
		}
		return false;
	}
}
